const app = getApp();

import config from '../../utils/config.js'
var APIURL = config.APIURL;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowHeight: 0,
    isVideo: false,//true：是视频 false：不是视频
    videoUrl: '',
    videoPosterUrl: '',
    swiperImg: [
      // 'https://static.56u56.com/Uploads/Picture/20181130/5c00f73411429.jpg'
    ],
    imageBtnUrl: '',//https://static.56u56.com/Uploads/Picture/20181224/5c2095885874f.png
    wxName: '',
    wxAppId: '',
    wxPath: '',
    identity: '',
    originId: ''
  },
  toMiniPr: function () {
    var self = this;

    console.log('wxName:' + self.data.wxName);
    console.log('wxAppId:' + self.data.wxAppId);
    console.log('wxPath:' + self.data.wxPath);

    //阿拉丁埋点
    app.aldstat.sendEvent('广告页-点击进入直播按钮', {
      wxName: self.data.wxName,
      wxAppId: self.data.wxAppId,
      wxPath: self.data.wxPath
    });

    wx.navigateToMiniProgram({
      appId: self.data.wxAppId,
      path: self.data.wxPath,
      // envVersion: 'develop',
      success(res) {

        //阿拉丁埋点
        app.aldstat.sendEvent('广告页-点击进入直播按钮-success', {
          wxName: self.data.wxName,
          wxAppId: self.data.wxAppId,
          wxPath: self.data.wxPath
        });

        // 打开成功,将该用户告知给后端
        // if (self.data.identity != '' && self.data.originId != '') {
        //   wx.request({
        //     url: APIURL + '/api/mp/fl/event/click',
        //     method: 'POST',
        //     header: {
        //       'Content-Type': 'application/json'
        //     },
        //     data: {
        //       identity: self.data.identity,
        //       originId: self.data.originId
        //     },
        //     success: res => {
        //       if (res.data.code == 200) {
        //       }
        //     }
        //   })
        // }

      },
      fail(res){
        
        //阿拉丁埋点
        app.aldstat.sendEvent('广告页-点击进入直播按钮-fail', {
          wxName: self.data.wxName,
          wxAppId: self.data.wxAppId,
          wxPath: self.data.wxPath
        });

      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;

    //获取视图高
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowHeight)
        self.setData({
          windowHeight: res.windowHeight
        })
      }
    })

    //获取用户唯一标识
    if (options.identity != undefined && options.identity != 'undefined' && options.identity != '' && options.identity != null && options.identity != 'null') {
      self.setData({
        identity: options.identity
      });
    }
    console.log('用户唯一标识：' + self.data.identity);
    //获取小程序原始ID
    if (options.originId != undefined && options.originId != 'undefined' && options.originId != '' && options.originId != null && options.originId != 'null') {
      self.setData({
        originId: options.originId
      });
    }
    console.log('小程序原始ID：' + self.data.originId);

    //阿拉丁埋点
    app.aldstat.sendEvent('广告页-进入广告页', {});

    //获取配置化参数
    wx.request({
      url: APIURL + '/api/mp/promote/fl2/data',
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      data: {},
      success: res => {
        if (res.data.code == 200 && res.data.data.type!=undefined) {

          console.log(res.data.data);
          var resData = res.data.data;

          if (resData.type == 1) {//图片

            //设置值
            self.setData({
              isVideo: false,
              imageBtnUrl: resData.btnImgUrl,
              swiperImg: resData.bgUrl,
              wxName: resData.wxName,
              wxAppId: resData.wxAppId,
              wxPath: resData.wxPath
            });

          } else {//视频

            //设置值
            self.setData({
              isVideo: true,
              imageBtnUrl: resData.btnImgUrl,
              videoUrl: resData.bgUrl,
              videoPosterUrl: resData.videoPosterUrl,
              wxName: resData.wxName,
              wxAppId: resData.wxAppId,
              wxPath: resData.wxPath
            });

          }

        } else {

          //设置默认值
          self.setData({
            isVideo: true,
            imageBtnUrl: 'https://static.56u56.com/Uploads/Picture/20181224/5c2095885874f.png',
            videoUrl: 'https://static.nbchhf.com/apk/qxx_v11_16.mp4',
            videoPosterUrl: 'https://static.56u56.com/Uploads/Picture/20181224/5c209a1f86ca4.png',
            wxName: '',
            wxAppId: '',
            wxPath: ''
          });

        }
      },
      fail: res => {
        var self = this;

        //设置默认值
        self.setData({
          isVideo: true,
          imageBtnUrl: 'https://static.56u56.com/Uploads/Picture/20181224/5c2095885874f.png',
          videoUrl: 'https://static.nbchhf.com/apk/qxx_v11_16.mp4',
          videoPosterUrl: 'https://static.56u56.com/Uploads/Picture/20181224/5c209a1f86ca4.png',
          wxName: '',
          wxAppId: '',
          wxPath: ''
        });

        // wx.showModal({
        //   title: '提示',
        //   content: '服务器繁忙，再试一次？',
        //   success: function (res) {
        //     if (res.confirm) {
        //       console.log('用户点击确定')
        //       self.onLoad();
        //     } else {
        //       console.log('用户点击取消')
        //       self.onLoad();
        //     }

        //   }
        // })

      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})